//Islsp
//$Id: islsp_user_omp.cpp 726 2012-12-14 19:51:50Z andrew.tsyganov $
#include "stdafx.h"

#include "projects/islsp/ga/islsp_ga.h"
#include "projects/islsp/sa/islsp_sa.h"
#include "src/heo/include/run_omp.h"
#include "src/heo/include/armadillo.h"

#define WEAK

#ifdef WEAK
#include "solvers/hybrids/weak_hybrid.h"
#endif

#include "solvers/ga/ga_omp.h"
#include "solvers/sa/sa_omp.h"

#include "solvers/user/user_omp.h"


template<>
struct solution<Islsp_Solution, user_tag>:
    public Islsp_Solution
{
//public:
};

template<class T>
class my_solver: public user_OMP<T, my_solver<T> >
{
    virtual void user_Solve(Solution_Pool<typename TEnvironment::TSolution>& pool,
        typename TEnvironment::TStatistics& stat, int np)
    {
        Array<double> c, t0 = current_.get_T();
        size_t n = current_.get_T().size();
        c.resize(n);

        //current_.get_T()[0] = 100;
        //current_.get_T()[1] = 200;
        //current_.Changed();
        //stat.update(0, rank_, *this, *this->env_);

        TCostType old_cost, new_cost;
        //double eps = 0.000001;

		std::cout << "GN initial point:" << std::endl;
		for (int i = 0; i < n; ++i)
			std::cout << t0[i] << " ";
		std::cout << std::endl;

		//current_.J_MLF(env_, rnd_);
        current_.evaluate_Correction_MLF(env_, c);
		
        std::cout << "GN initial point:" << std::endl;
		for (int i = 0; i < n; ++i)
			std::cout << t0[i] << " ";
		std::cout << std::endl;

		//current_.J_SR_MLF(env_, rnd_);
        current_.evaluate_Correction_SR_MLF(env_, c);

        size_t i = 0;

		do
        {
			break;
			std::cout << "i = " << i << std::endl;
            current_.evaluate(env_, rnd_);
            old_cost = current_.get_Cost(); 


			if (env_->cfg_.method_ == "fk")  
			{
				current_.evaluate_Correction_MLF(env_, c);
			}
			else
			{
				if (env_->cfg_.method_ == "sr")
				{
					current_.evaluate_Correction_SR_MLF(env_, c);
				}
			}

            //current_.evaluate_Correction_MLF(env_, c);
            //current_.evaluate_Correction_SR_MLF(env_, c);
 
		   //std::cout << "exit correction " << std::endl;
           for (size_t k = 0; k < n; ++k)
		   {
                current_.get_T()[k] -= c[k];
			    //std::cout << "c[" << k << "] = " << c[k] << std::endl;
		   }

            current_.Changed();
			//std::cout << "enter evaluate " << std::endl;
            current_.evaluate(env_, rnd_);
			//std::cout << "exit evaluate " << std::endl;
            new_cost = current_.get_Cost(); 
		   
			//std::cout << "exit get_Cost " << std::endl;

            stat.update(i, rank_, *this, *this->env_);
            ++i;
        }
        while (i < 800);
		//std::cout << "end!" << std::endl;
        //while ((abs(new_cost - old_cost) >= eps) && (i <= 40));
    }
};


typedef Islsp_Problem         TProblem;
typedef Islsp_Solution        TSolution;
typedef Islsp_Config          TConfig;
typedef Islsp_History         THistory; 

typedef Environment<TProblem, TSolution, TConfig, THistory>      TEnvironment;

typedef struct user_traits<TEnvironment> TMyTraits;
typedef my_solver<TMyTraits>             TSolverGN;


typedef struct GA_Traits<TEnvironment> TTraits1;
typedef GA_OMP<TTraits1>               TSolverGA;

typedef struct SA_Traits<TEnvironment> TTraitsSA;
typedef SA_OMP<TTraitsSA>               TSolverSA;

static struct ga_register
{
    ga_register()
    {
        TSolverGA::register_Selection< ga::selection::tournament_selection >();
        TSolverGA::register_Selection< ga::selection::roulette_selection >();
        TSolverGA::register_Selection< ga::selection::random_selection >();

        TSolverGA::register_Operator< ga::inter_operator::crossover>();
        TSolverGA::register_Operator< ga::inter_operator::mutation>();
    }
} reg;


int main(int argc, char* argv[])
{
#ifdef WEAK
    return Run_OMP< weak_hybrid<TSolverSA, TSolverGN, check_point_omp> >(argc, argv);
#else
    return Run_OMP< TMySolver >(argc, argv);
#endif
}
//EOF!
